import { instantiate, resources, _decorator, Component, Node, v3, UITransform, type EventTouch, Prefab } from "cc";
import { setLastPosition } from "./lastPosition";

const { ccclass, property } = _decorator;

@ccclass("Bg")
export class Bg extends Component {
  rect: Node = null;
  uiTrans: UITransform = null;

  protected onLoad(): void {
    // this.rect = this.node.getChildByName("rect");
    // this.uiTrans = this.node.getComponent(UITransform);
    // this.createPlanes(10);
  }
  start() {
    this.node.getChildByName()
    // this.node.on(Node.EventType.TOUCH_MOVE, this.setRectPosition, this);
    // this.node.on(Node.EventType.TOUCH_START, this.setRectPosition, this);
  }
  setRectPosition(e: EventTouch) {
    const u = e.getUILocation();
    const v = this.uiTrans.convertToNodeSpaceAR(v3(u.x, u.y, 0));
    setLastPosition(u);
    this.rect.setPosition(v);
  }

  createPlanes(count: number) {
    resources.load("prefab/plane", Prefab, (err, data) => {
      if (err) return;
      for (let i = 0; i < count; i++) {
        const plane = instantiate(data);
        plane.setParent(this.node);
      }
    });
  }
  update(deltaTime: number) {}
}
